<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

defined('IN_ADMIN_COMPONENT_CONTROLLER') or die();

require_once('includes/functions/configuration_functions.php');

class CacheComponent
{
	function CacheComponent()
	{
	}

	function action(&$params, $action='query')
	{
		if ($action == 'Clean')
			return $this->clean($params);
		else if ($action == 'CleanImages')
			return $this->cleanImages($params);
		else if ($action == 'Warm')
			return $this->warm($params);
		else		
			return $this->query($params);
	}

	function query(&$params)
	{
		$handle = get_class($this);

		$files = scandir(DIR_FS_CATALOG_CACHE);
		$cnt = 0;
		foreach ($files as $file) {
			if ($file == '.' || $file == '..' || $file == '.svn') continue;
			++$cnt;
		}

		$value{$handle}{0}{'count'}= $cnt;

		$files = scandir(DIR_FS_CATALOG_IMAGES . 'products');
		$cnt = 0;
		foreach ($files as $file) {
			if ($file == '.' || $file == '..' || $file == '.svn') continue;
			if (strlen($file) > 5 && substr($file,0,5) == 'cache') ++$cnt;
		}
		$files = scandir(DIR_FS_CATALOG_IMAGES . 'manufacturers');
		foreach ($files as $file) {
			if ($file == '.' || $file == '..' || $file == '.svn') continue;
			if (strlen($file) > 5 && substr($file,0,5) == 'cache') ++$cnt;
		}

		$value{$handle}{0}{'file_count'}= $cnt;

		return $value;
	}

	function clean(&$params)
	{
		clean_cache();
		return $this->query($params);
	}

	function cleanImages(&$params)
	{
		$files = scandir(DIR_FS_CATALOG_IMAGES . 'products');
		foreach ($files as $file) {
			if ($file == '.' || $file == '..' || $file == '.svn') continue;
			if (strlen($file) > 5 && substr($file,0,5) == 'cache') unlink(DIR_FS_CATALOG_IMAGES . 'products/' . $file);
		}
		$files = scandir(DIR_FS_CATALOG_IMAGES . 'manufacturers');
		foreach ($files as $file) {
			if ($file == '.' || $file == '..' || $file == '.svn') continue;
			if (strlen($file) > 5 && substr($file,0,5) == 'cache') unlink(DIR_FS_CATALOG_IMAGES . 'manufacturers/' . $file);
		}
		return $this->query($params);
	}

	function warm(&$params)
	{
		$sqlQuery = 'select products_id from ' . TABLE_PRODUCTS . ' order by products_id';
		$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
		while($row = mysql_fetch_array($dataReturned))
		{
			$productId = $row['products_id'];
			$handleUrl = fopen(HTTP_SERVER . '/index.php?productId='.$productId, 'r');
			fclose($handleUrl);
		}

		return $this->query($params);
	}
}
?>
